659F - Polycarp and Hay - CodeForces Solution


dfs and similar dsu graphs greedy sortings *2000

Please click on ads to support us..

Python Code:

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

from collections import defaultdict,deque
n,m,kq=map(int,input().split())
l=[]
for i in range(n):
    l.append(list(map(int,input().split())))
f=0
p=[i for i in range(n*m)]
size=[1 for i in range(n*m)]
def get(a):
    if p[a]!=a:
        p[a]=get(p[a])
    return p[a]
def union(a,b):
    a=get(a)
    b=get(b)
    if a==b:
        return
    if a>b:
        a,b=b,a
    p[a]=b
    size[b]+=size[a]
ind=defaultdict(list)
for i in range(n):
    for j in range(m):
        ind[l[i][j]].append(i*m+j)
f1=-1
for i in sorted(ind,reverse=True):
    maw=(0,0)
    for j in ind[i]:
        for k in [j-m,j+m,j-1,j+1]:
            if 0<=k<n*m and l[k//m][k%m]>=i:
                if m>1 and (k==j-1 or k==j+1):
                    if j//m==k//m:
                        union(j,k)
                else:
                    union(j,k)
        maw=max(maw,(size[get(j)],j))
    if i*maw[0]>=kq and kq%i==0:
        f=i
        f1=maw
        break
    if f!=0:
        break
if f!=0:
    print("YES")
    ax,by=f1[1]//m,f1[1]%m
    new=[[0 for i in range(m)]for j in range(n)]
    visited=[[0 for i in range(m)]for j in range(n)]
    q=deque([(ax,by)])
    c=0
    while(len(q)>0):
        x,y=q.popleft()
        new[x][y]=f
        if visited[x][y]==0:
            visited[x][y]=1
        c+=1
        if c==kq//f:
            break
        for i,j in [(x+1,y),(x,y-1),(x,y+1),(x-1,y)]:
            if 0<=i<n and 0<=j<m and l[i][j]>=f and visited[i][j]==0:
                visited[i][j]=1
                q.append((i,j))
    for i in range(n):
        print(*new[i])
else:
    print("NO")


Comments

Submit
0 Comments
More Questions

1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles